/****************************************************************************
**
** This file is part of qPhotoTag, a photo keywording application
**
** Copyright (C) 2009 Marcell Lengyel <miketkf@gmail.com>
**
** GNU General Public License
** This program is free software; you can redistribute it and/or
** modify it under the terms of the GNU General Public License
** as published by the Free Software Foundation; either version 2
** of the License, or (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
**
****************************************************************************/

/*! \file taglist.cpp
    \brief This file contains the implementation of the TagList class

    The TagList class is a QListView subclass that is used by the
    qPhotoTag application to display a list of IPTC keywords.
 */
#include <QtGui>
#include "taglist.h"

TagList::TagList(QWidget *parent)
	: QListView(parent)
{
    tag_list_model = new QStringListModel(this);
    this->setModel(tag_list_model);
    this->setSelectionMode(QListView::ExtendedSelection);
    this->setEditTriggers(QListView::NoEditTriggers);
#ifdef Q_WS_MAC
    this->setFrameStyle(QFrame::NoFrame);
    this->setAlternatingRowColors(true);
#endif
}

void TagList::focusInEvent(QFocusEvent *e)
{
    emit tagListFocusIn();
    QListView::focusInEvent(e);
}

void TagList::add_tag(const QString &newTag)
{
	if (! newTag.isEmpty()) {
		QStringList tagList = tag_list_model->stringList();
		if (! tagList.contains(newTag)) {
			int row = tag_list_model->rowCount();
			tag_list_model->insertRow(row);
			tag_list_model->setData(tag_list_model->index(row),newTag);
			tag_list_model->sort(0);
		}
	}
}

void TagList::clear()
{
	tag_list_model->removeRows(0,tag_list_model->rowCount());
}

bool TagList::is_selection()
{
	bool retval = false;
	if (! this->selectionModel()->selectedIndexes().empty())
		retval = true;
	return retval;
}

QStringList TagList::get_selected_tags()
{
	QStringList selected_tags;
	if (is_selection())
	{
		QModelIndex index;
		foreach(index, this->selectionModel()->selectedIndexes())
		{
			selected_tags.append(tag_list_model->data(index,Qt::DisplayRole).toString());
		}
	}
	return selected_tags;
}

void TagList::remove_selected_rows()
{
	
}
